package com.peternguyen.totolistpro.sql;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TodoSQLiteHelper extends SQLiteOpenHelper {

	public TodoSQLiteHelper(Context context) {
		super(context, "todos_db", null, 1);
	}

	/**
	 * Create simple table todos _id - key todo - todo text
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		// Execute create table SQL
		db.execSQL("CREATE TABLE tblAccount(_id INTEGER PRIMARY KEY AUTOINCREMENT, pwd TEXT NOT NULL, email TEXT NOT NULL);");
		db.execSQL("CREATE TABLE tblCategory(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, thumb_url BLOB NULL);");
		db.execSQL("CREATE TABLE tblConfig(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, value TEXT NOT NULL);");
		db.execSQL("CREATE TABLE tblTodo(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, notes TEXT NULL, create_date DATETIME NULL,deadline_date DATETIME NULL,priority INTEGER NULL, email TEXT NULL,catid INTEGER NULL);");

		// Tao du lieu mac dinh

		//create data account
		db.execSQL("INSERT INTO [tblAccount]([pwd],[email]) VALUES('123456','itnguyennam@gmail.com')");
		//Create data config
		db.execSQL("INSERT INTO [tblConfig]([name],[value]) VALUES('numday_duedate','1')");
		db.execSQL("INSERT INTO [tblConfig]([name],[value]) VALUES('newtoday','1')");
		db.execSQL("INSERT INTO [tblConfig]([name],[value]) VALUES('language','1')");
		//Create data Category
		db.execSQL(insertcat("Việc nhà"));
		db.execSQL(insertcat("Học tập"));
		db.execSQL(insertcat("Cơ quan"));
		db.execSQL(insertcat("Mua sắm"));
		
	}

	public String insertcat(String value) {
		String a = "INSERT INTO [tblCategory]([name]) VALUES('" + value + "');";
		return a;
	}
	
	

	/**
	 * Recreates table
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
		// DROP table
		db.execSQL("DROP TABLE IF EXISTS tblAccount");
		db.execSQL("DROP TABLE IF EXISTS tblCategory");
		db.execSQL("DROP TABLE IF EXISTS tblTodo");
		db.execSQL("DROP TABLE IF EXISTS tblConfig");
		// Recreate table
		onCreate(db);
	}

}
